import { ElNotification, ElMessageBox } from 'element-plus'
import nprogress from 'nprogress'
// 消息提示
export function toast(message, type = "success", dangerouslyUseHTMLString = false, duration = 3000) {
  ElNotification({
    message,
    type,
    dangerouslyUseHTMLString,
    duration
  })
}

// 显示全屏loading
export function showFullLoading() {
  nprogress.start()
}

// 隐藏全屏loading
export function hideFullLoading() {
  nprogress.done()
}


//显示确定组件
export function showModal(content = "提示内容", type = "warning", title = "") {
  return ElMessageBox.confirm(
    content,
    title,
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type,
    }
  )
}


//blob对象转化为base64
export function dtoToBase64(base64) {
  // 创建一个 FileReader 对象
  var reader = new FileReader();

  // 使用 FileReader 读取 Blob 对象的内容
  reader.readAsDataURL(base64);

  // 当读取完成时执行回调函数
  reader.onloadend = function () {
    // 将读取的结果转换为 base64 字符串
    var base64Data = reader.result;

    return base64Data
  };
}


//将baseURL转化Blob对象(适合baseURL没有错误，如果是用canvas获取的baseURL，建议直接转化为Blob对象)
export function convertBase64UrlToBlob(base64) {
  let urlData = base64.dataURL
  let type = base64.type
  let bytes = null
  if (urlData.split(',').length > 1) {//是否带前缀
    bytes = window.atob(urlData.split(',')[1]) // 去掉url的头，并转换为byte
  } else {
    bytes = window.atob(urlData)
  }
  // 处理异常,将ascii码小于0的转换为大于0
  let ab = new ArrayBuffer(bytes.length)
  let ia = new Uint8Array(ab)
  for (let i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i)
  }
  return new Blob([ab], { type: type })
}


//格式化时间
export function formatTime(time) {
  const date = new Date(time);
  return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
}

